import 'package:badges/badges.dart';
import 'package:flutter/material.dart';
import 'package:sample2/pages/chat/chat_conversation.dart';
import 'package:sample2/model/chat/chat_user.dart';

class ConversationListItem extends StatefulWidget {
  final ChatUser user;
  final bool isMessageRead;
  const ConversationListItem(
      {required this.user, required this.isMessageRead, super.key});
  @override
  State<StatefulWidget> createState() {
    return ConversationListItemState();
  }
}

class ConversationListItemState extends State<ConversationListItem> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {
          Navigator.push(context, MaterialPageRoute(builder: (context) {
            //
            return const ChatConversation();
          }));
        },
        child: Dismissible(
            key: const Key("conversation_item"),
            onDismissed: ((direction) {
              // setState(() {});
              ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
                content: Text("dismissed"),
              ));
            }),
            background: Container(
              color: Colors.red,
            ),
            child: Container(
              padding: const EdgeInsets.only(
                  left: 16, right: 16, top: 10, bottom: 10),
              height: 80,
              child: Row(children: <Widget>[
                Expanded(
                  child: Row(
                    children: <Widget>[
                      Badge(
                        showBadge: widget.user.unreadMessageCount != 0,
                        position: BadgePosition.topEnd(end: 0),
                        badgeContent: Text(
                          "${widget.user.unreadMessageCount}",
                          style: const TextStyle(color: Colors.white),
                        ),
                        child: const CircleAvatar(
                          radius: 30,
                          child: Text("JR"),
                        ),
                      ),
                      const SizedBox(
                        width: 16,
                      ),
                      Expanded(
                        child: Container(
                          color: Colors.transparent,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                widget.user.userName,
                                style: const TextStyle(fontSize: 16),
                              ),
                              const SizedBox(
                                height: 6,
                              )
                            ],
                          ),
                        ),
                      ),
                      Text(
                        widget.user.time,
                        style: TextStyle(
                            fontSize: 12,
                            fontWeight: widget.isMessageRead
                                ? FontWeight.bold
                                : FontWeight.normal),
                      ),
                    ],
                  ),
                )
              ]),
            )));
  }
}
